package com.aabte.design2.structural.wrapper;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/5/3
 */
public class CompressionDecorator extends DataSourceDecorator {
    public CompressionDecorator(DataSource wrapper) {
        super(wrapper);
    }

    @Override
    public void writeData(String data) {
        System.out.println("CompressionDecorator#writeData, data = " + data);
        data = compress(data);
        super.writeData(data);
    }

    @Override
    public String readData() {
        String data = super.readData();
        data = decompress(data);
        System.out.println("CompressionDecorator#readData, data = " + data);
        return data;
    }

    private String decompress(String data) {
        return data.replace("compression_", "");
    }

    private String compress(String data) {
        return "compression_" + data;
    }
}
